import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker_plus/picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sptapp/moudle/message.dart';

import '../../config/const.dart';

// const double kPickerHeight = 216.0;
// const double kItemHeight = 40.0;
const Color kBtnColor = Color(0xFF323232); //50
// const Color kTitleColor = Colors.white; //120
const double kTextFontSize = 17.0;

typedef StringClickCallback = void Function(int selectIndex, Object selectStr);
typedef ArrayClickCallback = void Function(
    List<int> selecteds, List<dynamic> strData);
typedef DateClickCallback = void Function(
    dynamic selectDateStr, dynamic selectDate);

enum DateType {
  YMD, // y, m, d
  YM, // y ,m
  YMD_HM, // y, m, d, hh, mm
  YMD_AP_HM, // y, m, d, ap, hh, mm
  HM,
}

class JhPickerTool {
  /** 单列*/
  static void showStringPicker<T>(
    BuildContext context, {
    required List<T> data,
    String? title,
    String? subTitle,
    String? desc,
    int? normalIndex,
    PickerDataAdapter? adapter,
    required StringClickCallback clickCallBack,
  }) {
    openModalPicker(
      context,
      adapter: adapter ??
          PickerDataAdapter<String>(
            pickerData: data,
          ),
      clickCallBack: (Picker picker, List<int> selecteds) {
        clickCallBack(selecteds[0], data[selecteds[0]] as Object);
      },
      selecteds: [normalIndex ?? 0],
      title: title,
      subTitle: subTitle,
      desc: desc
    );
  }

  /** 多列 */
  static void showArrayPicker<T>(
    BuildContext context, {
    required List<T> data,
    String? title,
        String? subTitle,
        String? desc,
        int tyepe = 0, // 0 ： 1 ·
    List<int>? normalIndex,
    PickerDataAdapter? adapter,
    required ArrayClickCallback clickCallBack,
  }) {
    openModalPicker(
      context,
      adapter: adapter ?? PickerDataAdapter(pickerData: data, isArray: true),
      clickCallBack: (Picker picker, List<int> selecteds) {
        clickCallBack(selecteds, picker.getSelectedValues());
      },
      selecteds: normalIndex,
      delimiter: [
        PickerDelimiter(
            child: Container(
              width: 48.w,
              color: Colors.transparent,
              alignment: Alignment.center,
            ),column: 0),
        PickerDelimiter(
            child: Container(
              width: 48.w,
              alignment: Alignment.center,
              child: Stack(
                children: [
                  Center(
                    child: Container(
                      height: 120.w,
                      width: 30.0,
                      decoration: BoxDecoration(
                          border: Border(
                            top: BorderSide(color: Color(0x2f999999),width: 1.w),
                            bottom: BorderSide(color: Color(0x2f999999),width: 1.w),
                          )
                      ),
                      child: Text(tyepe == 0?":":"·",style: TextStyle(
                          color: Colors.white,
                          fontSize: 40.sp,
                          fontWeight: FontWeight.w600
                      ),),
                      alignment: Alignment.center,
                    ),
                  )
                ],
              ),
            ),column: 2),
        PickerDelimiter(
            child: Container(
              width: 48.w,
              color: Colors.transparent,
              alignment: Alignment.center,
            ),column: 4),

      ],
      title: title,
      subTitle: subTitle,
      desc: desc,
    );
  }

  /** 日期选择器*/
  static void showDatePicker(
      BuildContext context, {
        required DateType dateType,
        String? title,
        String? subTitle,
        String? desc,
        DateTime? maxValue,
        DateTime? minValue,
        DateTime? value,
        DateTimePickerAdapter? adapter,
        required DateClickCallback clickCallback,
        Function? callBack,
      }) {
    int timeType;
    if (dateType == DateType.YM) {
      timeType = PickerDateTimeType.kYM;
    } else if (dateType == DateType.YMD_HM) {
      timeType = PickerDateTimeType.kYMDHM;
    } else if (dateType == DateType.YMD_AP_HM) {
      timeType = PickerDateTimeType.kYMD_AP_HM;
    }else if (dateType == DateType.HM) {
      timeType = PickerDateTimeType.kHM;
    } else {
      timeType = PickerDateTimeType.kMDY;
    }

    openModalPicker(
      context,
      adapter: adapter ??
          DateTimePickerAdapter(
            type: timeType,
            isNumberMonth: false,
            yearSuffix: "",
            monthSuffix: "",
            daySuffix: "",
            hourSuffix: "",
            minuteSuffix: "",
            strAMPM: const [],
            maxValue: maxValue,
            minValue: minValue,
            value: value ?? DateTime.now(),
          ),
      title: title,
      subTitle: subTitle,
      desc: desc,
      delimiter:dateType == DateType.HM ? [
        PickerDelimiter(
            child: Container(
              width: 48.w,
              color: Colors.transparent,
              alignment: Alignment.center,
            ),column: 0),
        PickerDelimiter(
            child: Container(
              width: 48.w,
              alignment: Alignment.center,
              child: Stack(
                children: [
                  Center(
                    child: Container(
                      height: 120.w,
                      width: 30.0,
                      decoration: BoxDecoration(
                          border: Border(
                            top: BorderSide(color: Color(0x2f999999),width: 1.w),
                            bottom: BorderSide(color: Color(0x2f999999),width: 1.w),
                          )
                      ),
                      child: Text(":",style: TextStyle(
                          color: Colors.white,
                          fontSize: 40.sp,
                          fontWeight: FontWeight.w600
                      ),),
                      alignment: Alignment.center,
                    ),
                  )
                ],
              ),
            ),column: 2),
        PickerDelimiter(
            child: Container(
              width: 48.w,
              color: Colors.transparent,
              alignment: Alignment.center,
            ),column: 4),

      ]:dateType == DateType.YMD ? [
        PickerDelimiter(
            child: Container(
              width: 48.w,
              color: Colors.transparent,
              alignment: Alignment.center,
            ),column: 0),
        PickerDelimiter(
            child: Container(
              width: 48.w,
              color: Colors.transparent,
              alignment: Alignment.center,
            ),column: 4),
      ]:[],
      clickCallBack: (Picker picker, List<int> selecteds) {
        var time = (picker.adapter as DateTimePickerAdapter).value;
        if(dateType == DateType.YMD && time!=null){
          var timeStr = formatDate(time!, [yy,mm,dd]);
          String month = formatDate(time!, [mm]);
          if(month == '01'){
            month = 'Jan';
          }else if(month == '02'){
            month = 'Feb';
          }else if(month == '03'){
            month = 'Mar';
          }else if(month == '04'){
            month = 'Apr';
          }else if(month == '05'){
            month = 'May';
          }else if(month == '06'){
            month = 'Jun';
          }else if(month == '07'){
            month = 'Jul';
          }else if(month == '08'){
            month = 'Aug';
          }else if(month == '09'){
            month = 'Sep';
          }else if(month == '10'){
            month = 'Oct';
          }else if(month == '11'){
            month = 'Nov';
          }else if(month == '12'){
            month = 'Dec';
          }
          String day = formatDate(time!, [dd]);
          String yaer = formatDate(time!, [yyyy]);
          clickCallback(timeStr, '$month/$day/$yaer');
        }
        else if(dateType == DateType.HM && time!=null){
          var timeStr = formatDate(time!, [HH,nn]);
          clickCallback(timeStr, formatDate(time!, [HH,':',nn,]));
        }
        if(time != null && callBack != null){
          callBack(time);
        }
      },
    );
  }


  static void openModalPicker(
    BuildContext context, {
    required PickerAdapter adapter,
    String? title,
    List<int>? selecteds,
    String? subTitle,
    String? desc,
        List<PickerDelimiter>? delimiter,
    required PickerConfirmCallback clickCallBack,
  }) {
    openModalPickerV2ORANGE(
      context,
      adapter: adapter,
      title: title,
      subTitle: subTitle,
      desc: desc,
      selecteds: selecteds,
      delimiter: delimiter,
      clickCallBack: clickCallBack,
    );
  }

  static void openModalPickerV2ORANGE(
    BuildContext context, {
    required PickerAdapter adapter,
    String? title,
    String? subTitle,
    String? desc,
        List<int>? selecteds,
    required PickerConfirmCallback clickCallBack,
    List<PickerDelimiter>? delimiter// 0: 日期选择器，1：时间选择器
  }) {
    Picker(
      height: 620.w,
      itemExtent: 120.w,
      looping: true,
      containerColor: Color(0xff1E1E1E),
      columnFlex: [1,1,1,1],
      delimiter: delimiter,
      selectionOverlay: Container(
        decoration: BoxDecoration(
          border: Border(
            top: BorderSide(color: Color(0x2f999999),width: 1.w),
            bottom: BorderSide(color: Color(0x2f999999),width: 1.w),
          )
        ),
      ),
      adapter: adapter,
      changeToFirst: false,
      textStyle: TextStyle(
        color: Color.fromRGBO(255, 255, 255, 0.5),
        fontSize: 40.sp,
        fontWeight: FontWeight.w400
      ),
      backgroundColor: Color(0xff1E1E1E),
      title:  Text(bottom_alert_left_title.tr,
          style: TextStyle(color: Colors.white, fontSize: kTextFontSize)),
      selecteds: selecteds,
      cancelTextStyle: TextStyle(color: kBtnColor, fontSize: kTextFontSize),
      confirmTextStyle: TextStyle(color: kBtnColor, fontSize: kTextFontSize),
      builderHeader: (BuildContext context) {
        return Container(
          padding: EdgeInsets.only(
            top: 32.w,
            left: 40.w,
            right: 40.w,
            bottom: 32.w
          ),
          decoration: BoxDecoration(
            color: Color(0xff1E1E1E),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(20.sp),
              topRight: Radius.circular(20.sp),
            ),
          ),
          child: Column(
            children: [
              Container(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(child: Container(
                      child: Text(
                        title??"",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 32.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    )),
                    SizedBox(width: 24.w,),
                    GestureDetector(
                      onTap: () async {
                        Navigator.pop<List<int>>(context, null);
                      },
                      child: Container(
                        child: Icon(Icons.close,size: 40.w,color: Color.fromRGBO(255, 255, 255, 0.5),),
                      ),
                    )
                  ],
                ),
              ),
              Visibility(child: Container(
                child: Row(
                  children: [
                    Expanded(child: Container(
                      child: Text(
                        subTitle??"",
                        style: TextStyle(
                            color: Color.fromRGBO(255, 255, 255, 0.5),
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w400,
                            height: 1.6
                        ),
                      ),
                    )),
                  ],
                ),
              ),visible: subTitle!=null&&subTitle.isNotEmpty,),
              Visibility(
                  visible: desc!=null && desc.isNotEmpty,
                  child: Container(
                    child: Column(
                      children: [
                        Container(
                          margin: EdgeInsets.symmetric(vertical: 32.w),
                          height: 1.w,
                          color: Color.fromRGBO(255, 255, 255, 0.1),
                        ),
                        Container(
                          child: Row(
                            children: [
                              Expanded(child: Container(
                                child: Text(
                                  desc??"",
                                  style: TextStyle(
                                      color: Color.fromRGBO(255, 255, 255, 0.5),
                                      fontSize: 24.sp,
                                      fontWeight: FontWeight.w400,
                                      height: 1.6
                                  ),
                                ),
                              )),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
              )
            ],
          ),
        );
      },
      textAlign: TextAlign.center,
      selectedTextStyle: TextStyle(
        color: Colors.white,
        fontSize: 48.sp,
        fontWeight: FontWeight.w400,
      ),
      footer: Container(
        width: 1.sw,
        color: Color(0xff1E1E1E),
        padding: EdgeInsets.only(
            bottom: MediaQuery.of(context).padding.bottom + 30.w),
        child: GestureDetector(
          onTap: () async {
            print('1231231231');
            Navigator.pop<List<int>>(context, selecteds);
            clickCallBack(adapter!.picker!, adapter.picker!.selecteds!);
          },
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 48.w),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(8.w)),
                color: themeColor),
            width: 690.w,
            height: 100.w,
            child: Center(
              child: Text(
                'Continue',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 30.sp,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ),
        ),
      ),
      onConfirm: clickCallBack,
      squeeze: 1.2
    ).showModal(context,isScrollControlled:true);
  }
}

